package com.example.flink.wordcount;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * Created with IntelliJ IDEA.
 * ClassName: StreamWord
 * Package: com.example.flink.wordcount
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-20
 * Time: 20:23
 */

//获取第一个单词
public class StreamWord {
    public static void main(String[] args) throws Exception {
        //1.创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //读取文件 源算子
        DataStreamSource<String> data = env.readTextFile("D:\\data\\hadoop\\Flink_review\\word.txt");

        //转换算子 map 提取第一个单词
        final SingleOutputStreamOperator<String> map = data.map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                //切割
                String[] s = value.split(" ");
                //返回第一个单
                return s[0];
            }
        });

        map.print();
        env.execute();

    }
}
